<template>
  <el-form label-width="80px" label-position="right">
    <el-row :gutter="8">
      <el-col :span="8">
        <el-form-item label="数据库">
          <el-select style="width: 100%" v-model="appScript.envProp">
            <template v-for="option in appContext.envs">
              <el-option v-if="option.envType === 'db'" :key="option.envProp" :value="option.envProp" :label="option.envLabel"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="16">
        <el-form-item label="脚本注释">
          <el-input v-model="appScript.scriptComment"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item label="脚本">
      <idea-code v-model="appScript.scriptBody" :config="{language:'mysql'}" style="height: calc(100vh - 465px)"></idea-code>
    </el-form-item>
    <el-form-item style="text-align: center">
      <el-button type="success" icon="el-icon-check" :loading="loading" @click="saveData(appScript)">{{appScript.scriptId ? '更新' : '保存'}}</el-button>
      <el-button v-if="appScript.scriptId" type="danger" icon="el-icon-caret-right" :loading="loading" @click="runScript">运行</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";
import IdeaCode from "@/components/idea-code/IdeaCode";

export default {
  name: "AppScriptInfo",
  components: {IdeaCode},
  mixins: [IdeaTable],
  inject: {
    appContext: {
      from: 'appContext',
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      appScript: {}
    }
  },
  created() {
    this.queryUrl = 'appScript/queryPage'
    this.saveUrl = 'appScript/save'
    this.pageInfo.pageSize = 1
    this.rowKey = 'scriptId'
    this.loadSource()
  },
  methods: {
    loadComplate() {
      this.$Util.cleanObj(this, this.appScript)
      if (this.dataSource.length > 0) {
        this.$Util.copyAttr(this.dataSource[0], this.appScript, null, this)
      }
    },
    runScript() {
      this.$get('/appScript/run/' + this.appScript.scriptId + '?appId=' + this.appInfo.appId)
      .then(res => {
        this.$resultMsg(this, res)
      })
    }
  }
}
</script>

<style scoped>

</style>